#!/bin/bash

# X-Admin 模块化架构测试脚本

echo "🚀 X-Admin 模块化架构测试"
echo "=================================="

# 检查编译
echo "📦 检查编译状态..."
if go build -o x-admin-test main.go; then
    echo "✅ 编译成功"
    rm -f x-admin-test
else
    echo "❌ 编译失败"
    exit 1
fi

# 检查模块结构
echo ""
echo "📁 检查模块结构..."
modules_dir="internal/modules"

if [ -d "$modules_dir" ]; then
    echo "✅ 模块目录存在: $modules_dir"
    
    # 检查各个模块
    modules=("auth" "user" "role" "permission" "upload")
    
    for module in "${modules[@]}"; do
        module_path="$modules_dir/$module"
        if [ -d "$module_path" ]; then
            echo "  ✅ $module 模块存在"
            
            # 检查模块文件
            files=("model.go" "repository.go" "service.go" "handler.go" "router.go")
            for file in "${files[@]}"; do
                if [ -f "$module_path/$file" ]; then
                    echo "    ✅ $file"
                else
                    echo "    ❌ $file 缺失"
                fi
            done
        else
            echo "  ❌ $module 模块缺失"
        fi
    done
else
    echo "❌ 模块目录不存在: $modules_dir"
fi

# 检查JWT包
echo ""
echo "🔐 检查JWT包..."
if [ -f "pkg/jwt/jwt.go" ]; then
    echo "✅ JWT包存在"
else
    echo "❌ JWT包缺失"
fi

# 检查工具包
echo ""
echo "🛠️ 检查工具包..."
if [ -f "pkg/utils/password.go" ]; then
    echo "✅ 密码工具存在"
else
    echo "❌ 密码工具缺失"
fi

# 检查中间件
echo ""
echo "🔒 检查中间件..."
if [ -f "internal/middleware/jwt.go" ]; then
    echo "✅ JWT中间件存在"
    
    # 检查是否包含AuthMiddleware函数
    if grep -q "AuthMiddleware" internal/middleware/jwt.go; then
        echo "✅ AuthMiddleware函数存在"
    else
        echo "❌ AuthMiddleware函数缺失"
    fi
else
    echo "❌ JWT中间件缺失"
fi

# 检查路由
echo ""
echo "🛣️ 检查路由..."
if [ -f "internal/router/router.go" ]; then
    echo "✅ 模块化路由存在"
else
    echo "❌ 模块化路由缺失"
fi

echo ""
echo "🎉 测试完成！"
echo ""
echo "📖 使用说明："
echo "1. X-Admin 使用模块化架构，路由: router.Init(db, cfg)"
echo "2. 查看架构文档: docs/modular-architecture.md"
echo "3. 开发指南: docs/migration-guide.md"
echo "4. 使用代码生成器: cd generator && go run main.go"
